package com.smasher.component.activity

import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.Canvas
import android.view.View
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat
import androidx.lifecycle.lifecycleScope
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import com.smasher.component.R
import com.smasher.component.databinding.ActivityNotificationBinding
import com.smasher.component.helper.NotificationHelper
import com.smasher.core.io.FileUtil
import com.smasher.core.log.Logger
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class NotificationActivity : NotificationInit(), View.OnClickListener {

    private lateinit var mBinding: ActivityNotificationBinding

    override fun getRootView(): View {
        mBinding = ActivityNotificationBinding.inflate(layoutInflater)
        return mBinding.root
    }

    override fun initView() {
        createNotificationChannel()
        mBinding.action1.setOnClickListener(this)
        mBinding.action2.setOnClickListener(this)
        mBinding.action3.setOnClickListener(this)
        mBinding.action4.setOnClickListener(this)
        mBinding.action5.setOnClickListener(this)
    }

    override fun initData() {

    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.action1 -> gotoNotificationSupport(
                "ntf01",
                "工单已分配1",
                "工单已经分配至特级维修工赵云"
            )

            R.id.action2 -> gotoNotificationSupport(
                "ntf02",
                "工单已分配2",
                "工单已经分配至特级维修工赵云"
            )

            R.id.action3 -> gotoNotificationSupport(
                "ntf03",
                "工单已分配3",
                "工单已经分配至特级维修工赵云"
            )

            R.id.action4 -> gotoNotificationSupport(
                "ntf04",
                "工单已分配4",
                "工单已经分配至特级维修工赵云"
            )

            R.id.action5 -> gotoNotificationSupport(
                "ntf05",
                "工单已分配5",
                "工单已经分配至特级维修工赵云"
            )

            else -> {}
        }
    }

    override fun createNotificationHelper(): NotificationHelper {
        return object : NotificationHelper() {

            override fun providerSmallIcon(): Int {
                return com.smasher.dialog.R.drawable.icon_select
            }

            override fun providerLargeIcon(context: Context): Bitmap {

                val vectorDrawableCompat =
                    VectorDrawableCompat.create(
                        resources,
                        com.smasher.dialog.R.drawable.icon_select,
                        null
                    )
                val bitmap = Bitmap.createBitmap(
                    vectorDrawableCompat!!.intrinsicWidth,
                    vectorDrawableCompat.intrinsicHeight,
                    Bitmap.Config.ARGB_8888
                )
                val canvas = Canvas(bitmap)
                vectorDrawableCompat.setBounds(0, 0, canvas.width, canvas.height)
                vectorDrawableCompat.draw(canvas)

//            val icon = ContextCompat.getDrawable(context, R.drawable.icon_select)
//            val bd = icon as BitmapDrawable
//            val bm = bd.bitmap

//            return BitmapFactory.decodeResource(
//                context.resources,
//                R.drawable.icon_select
//            )
                return bitmap
            }
        }
    }


    override suspend fun getNotificationConfig(): MutableList<ChannelParam> {
        return with(Dispatchers.IO) {
            try {
                val bytes = FileUtil.loadAsset(baseContext, "config.json")
                val typeToken = object : TypeToken<ArrayList<ChannelParam>>() {}
                val type = typeToken.type
                val gson = GsonBuilder().create()
                return@with gson.fromJson<ArrayList<ChannelParam>>(String(bytes), type)
            } catch (e: Exception) {
                e.printStackTrace()
            }
            return@with ArrayList<ChannelParam>()
        }
    }


    private fun createNotificationChannel() {
        lifecycleScope.launch {
            val notificationJson = getNotificationConfig()
            notificationHelper.createNotificationChannel(
                baseContext,
                createChannelByConfig(notificationJson)
            )
        }
    }


    private fun notificationIMessage(channelId: String, title: String, message: String) {
        val id = notificationHelper.notifyMessage(
            this, channelId, title, message
        )
        Logger.e("notification id = $id")
    }

    @SuppressLint("InlinedApi")
    private fun gotoNotificationSupport(channelId: String, title: String, message: String) {
        val permissionValue = Manifest.permission.POST_NOTIFICATIONS
        val result =
            ActivityCompat.checkSelfPermission(this, permissionValue)
        if (result == PackageManager.PERMISSION_GRANTED) {
            notificationIMessage(channelId, title, message)
        } else {
            permissionRequestLauncher.launch(permissionValue)
        }
    }


    private val permissionRequestLauncher =
        registerForActivityResult(ActivityResultContracts.RequestPermission()) {
            if (it) {
                Toast.makeText(this, "has Permission", Toast.LENGTH_SHORT).show()
            }
        }


}